package com.shoppingmall.fragment;

import java.io.File;

import com.example.shoppingmall.R;
import com.shopping.tools.Constant;
import com.shoppingmall.photowall.PhotoWallAnalyze;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
/**
 * 
 * @author zln
 * 照片墙的主页面
 *
 */
public class Fragment_PhotoWall extends Fragment implements
		OnCheckedChangeListener, OnClickListener {
	PhotoWallAnalyze mPhotoWallAnalyze;
	FragmentManager fm;
	FragmentTransaction ft;// 碎片事务
	private int i = 2;
	private View view;
	private RadioButton rb_Today, rb_Week, rb_Month;
	private ImageView img_camera;
	private RelativeLayout rel_Photo;
	private Button btnCancle, btnGallery, btnPhotograph;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_photowall, container, false);
		init();// 初始化数据
		return view;
	}

	private void init() {
		// TODO Auto-generated method
		rel_Photo = (RelativeLayout) view.findViewById(R.id.rel_Photo);
		btnPhotograph = (Button) view.findViewById(R.id.btnPhotograph);
		btnPhotograph.setOnClickListener(this);// 点击调用相机
		btnCancle = (Button) view.findViewById(R.id.btnCancle); // 点击取消
		btnCancle.setOnClickListener(this);
		btnGallery = (Button) view.findViewById(R.id.btnGallery); // 点击进入系统相册
		btnGallery.setOnClickListener(this);
		rb_Today = (RadioButton) view.findViewById(R.id.rb_Today);
		rb_Today.setOnCheckedChangeListener(this);
		rb_Week = (RadioButton) view.findViewById(R.id.rb_Week);
		rb_Week.setOnCheckedChangeListener(this);
		rb_Month = (RadioButton) view.findViewById(R.id.rb_Month);
		rb_Month.setOnCheckedChangeListener(this);
		img_camera = (ImageView) view.findViewById(R.id.img_camera);
		img_camera.setOnClickListener(this);
		rb_Month.setChecked(true);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if (isChecked)
			switch (buttonView.getId()) {
			case R.id.rb_Today:

				Constant.queryDate = "today";
				PhotoWallAnalyze();
				break;
			case R.id.rb_Week:

				Constant.queryDate = "week";
				PhotoWallAnalyze();
				break;
			case R.id.rb_Month:

				Constant.queryDate = "month";
				PhotoWallAnalyze();
				break;

			default:
				break;
			}

	}

	private void PhotoWallAnalyze() {
		fm = getFragmentManager();
		ft = fm.beginTransaction();
		mPhotoWallAnalyze = new PhotoWallAnalyze();
		ft.replace(R.id.framelayout_photowall, mPhotoWallAnalyze, "fsg");
		ft.commit();
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.img_camera://点击相机
			if (i % 2 == 0) {
				rel_Photo.setVisibility(View.VISIBLE);
				i++;
			} else {
				rel_Photo.setVisibility(View.GONE);
				i++;
			}
			break;
		case R.id.btnCancle://点击取消
			rel_Photo.setVisibility(View.GONE);
			break;
		case R.id.btnPhotograph:
			// 从拍照获取图片
			Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
					Environment.getExternalStorageDirectory(), "temp.jpg")));
			startActivityForResult(intent, 2);
			break;
		case R.id.btnGallery:
			// 从相册获取图片
			Intent intent2 = new Intent(Intent.ACTION_PICK, null);
			intent2.setDataAndType(
					MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
			startActivityForResult(intent2, 1);
			break;

		default:
			break;
		}
	}

}